package main

import (
	"log"
	"net/http"
	"net/url"
	// "regexp"
)

type Router struct{}

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	// 可以进一步的正则转发
	log.Printf("收到 %s 请求,访问 %s \n", req.Method, req.URL.String())
	theurl, err := url.Parse(req.URL.String())
	checkErr(err)
	log.Printf("url path is %s ,and query is %s ,and fragment is %s \n", theurl.Path, theurl.RawQuery, theurl.Fragment)
	if c, ok := mux[theurl.Path]; ok {
		req.ParseForm()
		c.Init(rw, req.Form)
		switch req.Method {
		case "GET":
			c.Get()
		case "POST":
			c.Post()
		default:
			log.Printf("Request method is %s", req.Method)
			// TODO: 404 page
		}

	}

}
